package com.gz.controller;

import com.gz.entity.City;
import com.gz.mapper.CityMapper;
import com.gz.service.CityService;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class LayuiController {
    
    @Autowired
    CityService cityService;
    
    /**
     *
     * @param file : MultipartFile 是 spring mvc 提供的上传文件的接口
     * @return
     * @throws Exception
     */
    @RequestMapping("/layuiFileUpload")
    @ResponseBody
    public Map<String, Object> layuiFileUpload(
            MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
//
//
////        // 上传文件的保存路径
////        String path = "E:/opt/web1/files";
//
//        //  上传文件的保存路径
//        String path = request.getRealPath("/static/img");
//
//        // Original Filename 原始文件名
//        String fileName = file.getOriginalFilename() + "_" +UUID.randomUUID().toString();
//
//        // 对应磁盘上的一个 抽象 的对象
//        // 抽象的映射关系
//        File filePath = new File(path, fileName);
//
//        // 具体 filePath 文件的父路径是否存在
//        // 如果不存在, 则 调用这个方法 mkdirs() 创建
//        if (!filePath.getParentFile().exists()) {
//            filePath.getParentFile().mkdirs();
//        }
//
//        // 写入文件
//        // filePath 文件写入到磁盘上面
//        // FileOutputStream
//        file.transferTo(filePath);
    
    
        Configuration conf = new Configuration(true);
        conf.set("fs.defaultFS", "hdfs://localhost:8020");
    
        FileSystem fs = FileSystem.get(conf);
//        FileInputStream fis = new FileInputStream(filePath);
//        InputStream input = new BufferedInputStream(fis);
        Path f = new Path("");
        FSDataOutputStream output = fs.create(f);
        IOUtils.copyBytes(file.getInputStream(), output, conf, true);
        
        
//        Map<String, Object> map = new HashMap();
//        map.put("code", "0");
//        map.put("msg", "0");
//
//        Map<String, String> dataMap = new HashMap();
//        dataMap.put("src", "/static/img/" + fileName);
//        map.put("data", dataMap);
        
        return null;
        
    }
    
    @RequestMapping("getAllProvinces")
    @ResponseBody
    public List<City> getAllProvinces() throws Exception{
        return cityService.selectByParentId("-1");
    }
    
    @RequestMapping("getAllCityById")
    @ResponseBody
    public List<City> getAllCityById(String provinceId) throws Exception{
        return cityService.selectByParentId(provinceId);
    }
    
    @RequestMapping("getAllCountryById")
    @ResponseBody
    public List<City> getAllCountryById(String cityId) throws Exception{
        return cityService.selectByParentId(cityId);
    }

}
